package com.reverse.engineering.util.core.run.util;

import java.util.ArrayList;
import java.util.Arrays;

import com.ApplicationProperties;
import com.reverse.engineering.util.core.run.bean.java.File;
import com.reverse.engineering.util.core.run.constant.Constant;
import com.reverse.engineering.util.core.run.enums.FieldEquals;
import com.reverse.engineering.util.core.run.enums.FileGenerate;

/**
 * 初始化程序静态数据类
 * 
 * @author liu
 * @date 2020-08-20
 */
public class InitStaticField {

    public static void init() {
        initApplicationProperties();
        initGeneratePath();
        initPath();
        initFieldEquals();
    }

    /**
     * 初始化启动类属性
     */
    private static void initApplicationProperties() {
        ApplicationProperties.id = ApplicationProperties.id.toLowerCase();
        ApplicationProperties.serial_number = ApplicationProperties.serial_number.toLowerCase();
        ApplicationProperties.is_del = ApplicationProperties.is_del.toLowerCase();
        ApplicationProperties.databaseTablePrefix = ApplicationProperties.databaseTablePrefix.toLowerCase();
        ApplicationProperties.databaseTableSuffix = ApplicationProperties.databaseTableSuffix.toLowerCase();
    }

    /**
     * 初始化文件生成路径
     */
    private static void initGeneratePath() {
        if (ApplicationProperties.basePath != null) {
            ApplicationProperties.basePath = ApplicationProperties.basePath.replace(" ", "");
            if (ApplicationProperties.basePath.length() == 0) {
                ApplicationProperties.basePath = null;
            }
        }
        if (ApplicationProperties.entity != null) {
            FileGenerate.ENTITY.setBasePath(ApplicationProperties.entity.replace(" ", ""));
        }
        if (ApplicationProperties.daoInterface != null) {
            FileGenerate.DAO_INTERFACE.setBasePath(ApplicationProperties.daoInterface.replace(" ", ""));
        }
        if (ApplicationProperties.daoImpl != null) {
            FileGenerate.DAO_IMPL.setBasePath(ApplicationProperties.daoImpl.replace(" ", ""));
        }
        if (ApplicationProperties.serviceInterface != null) {
            FileGenerate.SERVICE_INTERFACE.setBasePath(ApplicationProperties.serviceInterface.replace(" ", ""));
        }
        if (ApplicationProperties.serviceImpl != null) {
            FileGenerate.SERVICE_IMPL.setBasePath(ApplicationProperties.serviceImpl.replace(" ", ""));
        }
        if (ApplicationProperties.controllerImpl != null) {
            FileGenerate.CONTROLLER_IMPL.setBasePath(ApplicationProperties.controllerImpl.replace(" ", ""));
        }
        if (ApplicationProperties.xml != null) {
            FileGenerate.XML.setBasePath(ApplicationProperties.xml.replace(" ", ""));
        }
        if (ApplicationProperties.sql != null) {
            FileGenerate.SQL.setBasePath(ApplicationProperties.sql.replace(" ", ""));
        }
    }

    /**
     * 初始文件生成路径
     * 
     * @return
     */
    public static void initPath() {
        FileGenerate[] values = FileGenerate.values();
        for (FileGenerate fileGenerate : values) {
            // 这一步不用看,就是为了替换成统一的classPath,因为在枚举类中没办法互相调用
            fileGenerate
                .setBasePath(fileGenerate.getBasePath().replace("classPath:", FileGenerate.CLASSPATH.getBasePath()));
            // 重点
            fileGenerate.setBasePath(initPath(fileGenerate.getBasePath()));
        }
        if (ApplicationProperties.basePath != null && ApplicationProperties.basePath.length() > 0) {
            ApplicationProperties.basePath = initPath(ApplicationProperties.basePath);
            File.basepathX = ApplicationProperties.basePath;
            File.basepathD = ApplicationProperties.basePath.replace("/", ".");
            File.basepathXX = File.basepathX + "/";
            File.basepathDD = File.basepathD + ".";
            for (FileGenerate fileGenerate : values) {
                String basePath = fileGenerate.getBasePath();
                // 不修改开头是classpath:的路径
                if (basePath.startsWith(FileGenerate.CLASSPATH.getBasePath())) {
                    continue;
                }
                if (basePath.length() == 0) {
                    fileGenerate.setBasePath(ApplicationProperties.basePath);
                } else {
                    fileGenerate.setBasePath(ApplicationProperties.basePath + "/" + basePath);
                }
            }
        }
    }

    /**
     * 初始化文件生成路径
     * 
     * @param path
     * @return
     */
    public static String initPath(String path) {
        // 替换成统一的路径格式
        path = path.replace(".", "/");
        path = path.replace("\\", "/");
        // 如果开头是斜线就去掉
        if (path.indexOf(":/") == -1 && path.startsWith("/")) {
            path = path.substring(1);
        }

        // 如果classPath:后开头是斜线就去掉
        String classPath = FileGenerate.CLASSPATH.getBasePath();
        if (path.startsWith(classPath) && path.length() > classPath.length()
            && path.charAt(classPath.length()) == '/') {
            path = classPath + path.substring(classPath.length() + 1);
        }
        // 如果最后是斜线就去掉
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }

    /**
     * 初始化数据库字段与Java字段对应关系
     */
    public static void initFieldEquals() {
        ArrayList<FieldEquals> fieldEquals = Constant.fieldEquals;
        fieldEquals.addAll(Arrays.asList(FieldEquals.values()));
    }

}